package com.zhilingshenghuo.app.zhly.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.zhilingshenghuo.app.zhly.R;

public class AlertUtils {

    private static AlertDialog alertDialog;

    public static AlertDialog normalAlert(final Activity context, String title,AlertListener alertListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_normal, null);
        TextView textView = view.findViewById(R.id.title);
        textView.setText(title);
        builder.setView(view);
        alertDialog = builder.create();
        Window window = alertDialog.getWindow();
        if (window != null) {
            window.setBackgroundDrawableResource(R.drawable.shape_dialog_normal);
        }
        view.findViewById(R.id.sure).setOnClickListener(v -> alertListener.sure());
        view.findViewById(R.id.cancel).setOnClickListener(v -> alertListener.onCancel());
        alertDialog.show();

        WindowManager m = context.getWindowManager();
        //为获取屏幕宽、高
        Display d = m.getDefaultDisplay();
        //获取对话框当前的参数值
        WindowManager.LayoutParams p = alertDialog.getWindow().getAttributes();
        p.width = (int) (d.getWidth() * 0.72);
        alertDialog.getWindow().setAttributes(p);
        return alertDialog;
    }

    public interface AlertListener{
        void sure();
        void onCancel();
    }


    public static void dismiss() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
            alertDialog = null;
        }
    }
}
